﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Windows;
using Timetable.AWP.Host.Course;
using Timetable.AWP.Host.Faculty;
using Timetable.AWP.Host.Group;
using Timetable.AWP.Host.Lecturer;
using Timetable.AWP.Host.Schedule;
using Timetable.AWP.Host.ScheduleInfo;
using Timetable.AWP.Host.Speciality;
using Timetable.AWP.Host.Time;
using Timetable.AWP.Host.Tutorial;
using Timetable.AWP.Host.TutorialType;

namespace Timetable.AWP.ViewModels
{
    public class BaseViewModel : DependencyObject, INotifyPropertyChanged
    {
        #region Services
        protected readonly IFacultyService FacultyService;
        protected readonly ICourseService CourseService;
        protected readonly IGroupService GroupService;
        protected readonly ITutorialService TutorialService;
        protected readonly ITutorialTypeService TutorialTypeService;
        protected readonly ISpecialityService SpecialityService;
        protected readonly ITimeService TimeService;
        protected readonly ILecturerService LecturerService;
        protected readonly IScheduleService ScheduleService;
        protected readonly IScheduleInfoService ScheduleInfoService;
        #endregion

        public BaseViewModel()
        {
            FacultyService = GetService<IFacultyService>("FacultyServiceAddress");
            CourseService = GetService<ICourseService>("CourseServiceAddress");
            GroupService = GetService<IGroupService>("GroupServiceAddress");
            TutorialService = GetService<ITutorialService>("TutorialServiceAddress");
            TutorialTypeService = GetService<ITutorialTypeService>("TutorialTypeServiceAddress");
            SpecialityService = GetService<ISpecialityService>("SpecialityServiceAddress");
            TimeService = GetService<ITimeService>("TimeServiceAddress");
            LecturerService = GetService<ILecturerService>("LecturerServiceAddress");
            ScheduleService = GetService<IScheduleService>("ScheduleServiceAddress");
            ScheduleInfoService = GetService<IScheduleInfoService>("ScheduleInfoServiceAddress");

            //FacultyService = new FacultyServiceClient();
            //CourseService = new CourseServiceClient();
            //GroupService = new GroupServiceClient();
            //TutorialService = new TutorialServiceClient();
            //TutorialTypeService = new TutorialTypeServiceClient();
            //SpecialityService = new SpecialityServiceClient();
            //TimeService = new TimeServiceClient();
            //LecturerService = new LecturerServiceClient();
            //ScheduleInfoService = new ScheduleInfoServiceClient();
            //ScheduleService = new ScheduleServiceClient();
        }

        protected T GetService<T>(string serviceName)
        {
            var url = ConfigurationManager.AppSettings[serviceName];

            if(String.IsNullOrEmpty(url))
                throw new ArgumentNullException("url");

            var myBinding = new BasicHttpBinding();
            var myEndpoint = new EndpointAddress(url);
            var myChannelFactory = new ChannelFactory<T>(myBinding, myEndpoint);
            var host = myChannelFactory.CreateChannel();

            return host;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
